草庐IT

Where 条件

全部标签

go - 最简单的 Go 竞争条件示例?

我需要一个简单的Go代码示例,它肯定会使程序进入竞争状态。有什么想法吗? 最佳答案 原问题:IneedasimpleGocodesamplewhichwilldefinitelyruntheprogramintoanrace-condition.例如,racer.go:packagemainimport("time")varcountintfuncrace(){count++}funcmain(){gorace()gorace()time.Sleep(1*time.Second)}输出:$gorun-raceracer.go====

http - 无法在 if..else 条件 : "undefined err go" 中获取变量数据

我正在尝试通过使用if..else条件来使用HTTPGet,但出现错误:undefined:errgo这是我的代码:packagemainimport("fmt""net/http")funcmain(){num:=0ifnum==0{resp,err:=http.Get("https://httpbin.org/get")}else{resp,err:=http.Get("https://google.com")}iferr!=nil{fmt.Println("error")}fmt.Println(resp.StatusCode)}我尝试在调用之前定义变量:varerrerrorv

go - "Declared but not used"for条件表达式中的变量

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭6年前。Improvethisquestion我的直觉方法:https://play.golang.org/p/6xzg7TK1IH它不起作用。你能分享一些替代方法吗?

multithreading - 为什么这段代码没有达到竞争条件?

我有这个go代码,它遍历目录文件树并生成其中每个文件的MD5哈希值,并将结果写入输出文件。packagemainimport("crypto/md5""encoding/hex""fmt""io""os""path/filepath""sync")funcmain(){filePath:=os.Args[1]output:=os.Args[2]wg:=&sync.WaitGroup{}err:=filepath.Walk(filePath,func(pathstring,infoos.FileInfo,errerror)error{if!info.IsDir(){wg.Add(1)go

go - 字符串的条件拆分

我想拆分'a/b/c'类型的字符串并允许使用'\'进行转义。例如:'foo/bar\/2.2/baz':a=foob=bar/2.2c=baz有什么优雅的方法可以用'/'分割,忽略'\/'吗? 最佳答案 无论您使用何种语言,您都可以使用两种基本方法。搜索所有出现的/但前面没有紧跟\并执行拆分。将\/的所有实例替换为一些不包含/的唯一符号,然后在/上拆分,并替换唯一符号再次使用\/。从计算的角度来看,前者会更有效率。从编码复杂性的角度来看,后者可能更容易编写。 关于go-字符串的条件拆分,

go - go 中具有依赖条件的动态数据结构(嵌套 json)

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭4年前。Improvethisquestion我正在尝试在go中创建动态嵌套的json。我知道go是静态类型,有多种方法可以创建动态对象(接口(interface)),我想知道是否有办法解决我在嵌套json中的依赖映射样本json[{"display":"Environment","field":"

go - 发送到 channel 时避免竞争条件?

go版本go1.11.2darwin/amd64我有以下代码示例,是为SO演示目的而创建的:packagemainimport(...)typeTstruct{ctxcontext.Contextch1chanstring}funcNew(ctxcontext.Context)*T{t:=&T{ctx:ctx}got.run(2)returnt}func(t*T)run(workersint){t.ch1=make(chanstring)done:=make(chanstruct{})gofunc(){当我使用竞争检测器构建并运行它时,它会抛出以下数据竞争:gobuild-race./

pointers - 可选择将 .Where() 应用于 gorm.DB

我是Go的新手,我的指针知识已经生锈了。我想改变gorm.DB的一个实例,以便我可以对其应用0个或多个Where子句。func(){db:=gorm.Open(/*...*/)err:=applyWhere(db,filters).Order("created_datetimedesc").Find(&rMessages).Error//...}funcapplyWhere(db*gorm.DB,filtersFilters)*gorm.DB{iffilters.MessageType!=""{db=db.Where(&message{MessageType:string(filter

go - 如何根据 golang 中的条件从 json 对象数组中获取值?

我有一个这样的数组。[{"seq":2,"amnt":125},{"seq":3"amnt":25},{"seq":2"amnt":250}]我需要从这个seq为2的数组中获取对象。在Linq中,我们有扩展,我可以在其中放置where条件。在Go中,我需要循环并使用for循环获取它还是有其他方法?请建议我一个最佳的方法。注意:json有很多字段,这个例子我只给了两个。我是围棋的新手。 最佳答案 我不知道执行此操作的“最佳”方法,但这是您现在可以做的事情:packagemainimport("encoding/json""fmt")f

go - 在 golang 中运行多个条件函数

我想在golang中使用5个函数来运行工作流函数初始化验证过程执行完成如果失败,每个方法都应该返回相同的结果对象和错误对象我想找到一种模式来运行此工作流,而不是执行以下操作:ifresult,err:=init();err!=nil{ifresult,err:=validate();err!=nil{ifresult,err:=process();err!=nil{ifresult,err:=execute();err!=nil{ifresult,err:=finalize();err!=nil{}}}}}提前致谢彼得 最佳答案 您